package cn.itcast.bos.service.take_delivery.imp;


import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.index.query.QueryStringQueryBuilder.Operator;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.WildcardQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.take_delivery.WayBillDao;
import cn.itcast.bos.domain.take_delivery.WayBill;
import cn.itcast.bos.index.WayBillIndexRepository;
import cn.itcast.bos.service.take_delivery.WayBillService;

@Service
@Transactional
public class WayBillServiceImp implements WayBillService {

	@Autowired
	private WayBillDao wayBillDao;
	
	@Autowired
	private WayBillIndexRepository wayBillIndexRepository;
	@Override
	public void save(WayBill model) {
		WayBill findBywayBillNum = wayBillDao.findBywayBillNum(model.getWayBillNum());
		if ( findBywayBillNum==null||findBywayBillNum.getId() == null ) {
			//设置运单状态
			 model.setSignStatus(1);
			// 说明运单主键不存在直接保存
			wayBillDao.save(model);
			//保存索引
			wayBillIndexRepository.save(model);
		} else {
			try {
				if(findBywayBillNum.getSignStatus()==1){
					Integer id = findBywayBillNum.getId();
					BeanUtils.copyProperties(findBywayBillNum, model);
					findBywayBillNum.setId(id);
					findBywayBillNum.setSignStatus(1);
					//保存索引
					wayBillIndexRepository.save(model);
				}
				
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				throw new  RuntimeException(e.getMessage());
			}
		}

	}

	@Override
	public Page<WayBill> findAll(WayBill wayBill ,Pageable pageable) {
		//判断WayBill条件是否存在
		 if(StringUtils.isBlank(wayBill.getWayBillNum())&&
		    StringUtils.isBlank(wayBill.getSendProNum())&&
			StringUtils.isBlank(wayBill.getSendAddress())&&
			StringUtils.isBlank(wayBill.getRecAddress())&&
			(wayBill.getSignStatus()==null||wayBill.getSignStatus()==0)){
			 //没有查询条件直接查询数据库
			 return wayBillDao.findAll(pageable);
		 }else{
			 //查询条件
			 //must条件必须成立相当于 AND
			 //must not 条件必须不成立 not 
			 //should 条件可以成立 or
			 BoolQueryBuilder  queryBuilder = new BoolQueryBuilder();
			 //向组合查询对象添加条件
			 if(StringUtils.isNotBlank(wayBill.getWayBillNum())){
				 //运单号查询
				 System.out.println(wayBill.getWayBillNum());
				 QueryBuilder builder = new TermQueryBuilder("wayBillNum",wayBill.getWayBillNum());//布尔查询
				 queryBuilder.must(builder);
				
			 }
			 if(StringUtils.isNotBlank(wayBill.getSendAddress())){
				 
				 //发货地查询  模糊查询 
				 //情况一.输入'北',是查询词条一部分,使用模糊匹配词条查询
				 QueryBuilder builder = new WildcardQueryBuilder("sendAddress", "*"+wayBill.getSendAddress()+"*");
				 //情况二:输入'北京市'海淀区是多个词条组合,进行分词后每个词条匹配查询
				 QueryBuilder queryBuilder2=new QueryStringQueryBuilder(wayBill.getSendAddress()).field("sendAddress")
						 .defaultOperator(Operator.AND);
				 //两种情况去OR关系
				 BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
				 boolQueryBuilder.should(builder);
				 boolQueryBuilder.should(queryBuilder2);
				 queryBuilder.must(boolQueryBuilder);
			 }
			 if(StringUtils.isNotBlank(wayBill.getRecAddress())){
				 //发货地查询
				 QueryBuilder builder = new WildcardQueryBuilder("recAddress", "*"+wayBill.getRecAddress()+"*");
				 //情况二:输入'北京市'海淀区是多个词条组合,进行分词后每个词条匹配查询
				 QueryBuilder queryBuilder2=new QueryStringQueryBuilder(wayBill.getRecAddress()).field("recAddress")
						 .defaultOperator(Operator.AND);
				 //两种情况去OR关系
				 BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
				 boolQueryBuilder.should(builder);
				 boolQueryBuilder.should(queryBuilder2);
				 queryBuilder.must(boolQueryBuilder);
			 }
			 if(StringUtils.isNotBlank(wayBill.getSendProNum())){
				 //速运类型等值查询
				 QueryBuilder builder = new TermQueryBuilder("sendProNum",wayBill.getSendProNum());//布尔查询
				 queryBuilder.must(builder);
			 }
			 if(wayBill.getSignStatus()!=null&&wayBill.getSignStatus()!=0){
				 //签收状态查询
				 QueryBuilder builder = new TermQueryBuilder("signStatus",wayBill.getSignStatus());//布尔查询
				 queryBuilder.must(builder);
			 }
			 
			 SearchQuery searchQuery =new NativeSearchQuery(queryBuilder);
			 searchQuery.setPageable(pageable);//分页效果
			 //有条件查询,向索引库查询
			 Page<WayBill> search = wayBillIndexRepository.search(searchQuery);
			 System.out.println(search.getContent().size()+"____________________________");
			 return search;
			 
		 }
		

		
	}

	@Override
	public WayBill findBywayBillNum(String wayBillNum) {

		return wayBillDao.findBywayBillNum(wayBillNum);
	}

	@Override
	public List<WayBill> findAll(WayBill wayBill) {
		//判断WayBill条件是否存在
		 if(StringUtils.isBlank(wayBill.getWayBillNum())&&
		    StringUtils.isBlank(wayBill.getSendProNum())&&
			StringUtils.isBlank(wayBill.getSendAddress())&&
			StringUtils.isBlank(wayBill.getRecAddress())&&
			(wayBill.getSignStatus()==null||wayBill.getSignStatus()==0)){
			 //没有查询条件直接查询数据库
			 return wayBillDao.findAll();
		 }else{
			 //查询条件
			 //must条件必须成立相当于 AND
			 //must not 条件必须不成立 not 
			 //should 条件可以成立 or
			 BoolQueryBuilder  queryBuilder = new BoolQueryBuilder();
			 //向组合查询对象添加条件
			 if(StringUtils.isNotBlank(wayBill.getWayBillNum())){
				 //运单号查询
				 System.out.println(wayBill.getWayBillNum());
				 QueryBuilder builder = new TermQueryBuilder("wayBillNum",wayBill.getWayBillNum());//布尔查询
				 queryBuilder.must(builder);
				
			 }
			 if(StringUtils.isNotBlank(wayBill.getSendAddress())){
				 
				 //发货地查询  模糊查询 
				 //情况一.输入'北',是查询词条一部分,使用模糊匹配词条查询
				 QueryBuilder builder = new WildcardQueryBuilder("sendAddress", "*"+wayBill.getSendAddress()+"*");
				 //情况二:输入'北京市'海淀区是多个词条组合,进行分词后每个词条匹配查询
				 QueryBuilder queryBuilder2=new QueryStringQueryBuilder(wayBill.getSendAddress()).field("sendAddress")
						 .defaultOperator(Operator.AND);
				 //两种情况去OR关系
				 BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
				 boolQueryBuilder.should(builder);
				 boolQueryBuilder.should(queryBuilder2);
				 queryBuilder.must(boolQueryBuilder);
			 }
			 if(StringUtils.isNotBlank(wayBill.getRecAddress())){
				 //发货地查询
				 QueryBuilder builder = new WildcardQueryBuilder("recAddress", "*"+wayBill.getRecAddress()+"*");
				 //情况二:输入'北京市'海淀区是多个词条组合,进行分词后每个词条匹配查询
				 QueryBuilder queryBuilder2=new QueryStringQueryBuilder(wayBill.getRecAddress()).field("recAddress")
						 .defaultOperator(Operator.AND);
				 //两种情况去OR关系
				 BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
				 boolQueryBuilder.should(builder);
				 boolQueryBuilder.should(queryBuilder2);
				 queryBuilder.must(boolQueryBuilder);
			 }
			 if(StringUtils.isNotBlank(wayBill.getSendProNum())){
				 //速运类型等值查询
				 QueryBuilder builder = new TermQueryBuilder("sendProNum",wayBill.getSendProNum());//布尔查询
				 queryBuilder.must(builder);
			 }
			 if(wayBill.getSignStatus()!=null&&wayBill.getSignStatus()!=0){
				 //签收状态查询
				 QueryBuilder builder = new TermQueryBuilder("signStatus",wayBill.getSignStatus());//布尔查询
				 queryBuilder.must(builder);
			 }
			 
			 SearchQuery searchQuery =new NativeSearchQuery(queryBuilder);
			 Pageable pageable =new PageRequest(0, 10000);
			 searchQuery.setPageable(pageable);
			 //有条件查询,向索引库查询
			 Page<WayBill> search = wayBillIndexRepository.search(searchQuery);
			 
			 return search.getContent();
			 
		 }
		 
		 
	}

}
